package com.microstation.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.microstation.common.AppConst;
import com.microstation.common.Result;
import com.microstation.enums.ResultEnum;
import com.microstation.util.CookieUtil;
import com.microstation.util.ResponseUtil;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SigninCookieInterceptor extends HandlerInterceptorAdapter {
    public static final String LAST_PAGE = "";
    private static final Logger log = Logger.getLogger(LoginInterceptor.class);// 日志文件

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String qrcode = request.getParameter("qrcode");
        Cookie cookie = CookieUtil.getCookieByName(request, AppConst.COOKIE_SIGN_NAME);
        //System.out.println("===="+JSONObject.toJSONString(cookie));
        if (cookie == null) {
            Result result = new Result();
            result.setHasError(true);
            result.setResultEnum(ResultEnum.NOSESSION);
            result.setErrUrl("login_signin.html?qrcode=" + qrcode);
            ResponseUtil.write(response, JSONObject.toJSONString(result));
            return false;
        }
        return true;
    }
}
